using System;
using System.Data;

namespace Common
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public struct TableColumn : IComparable  
    {
        /// <summary></summary>
        public string ColumnName;
        /// <summary></summary>
        public int Ordinal;
        /// <summary></summary>
        public System.Data.SqlDbType DbType;
        /// <summary></summary>
        public bool AllowNull;
        /// <summary></summary>
        public bool IsIdentity;
        /// <summary></summary>
        public bool IsPK;
        /// <summary></summary>
        public int Precision;
        /// <summary></summary>
        public int Scale;
        /// <summary></summary>
        public int Size;
        /// <summary></summary>
        public bool IsFK;
        /// <summary></summary>
        public bool IsIndexed;

        //public string ToDbTypeString()
        //{
        //    return DbTypeHelper.getDbDataTypeName(DataProviderType.MSSQL, this.DbType, this.Size, this.Precision, this.Scale);
        //}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="provider"></param>
        /// <returns></returns>
        public FieldDataType GetFieldDbType(DataProviderType provider)
        {
            FieldDataType fieldDbtype = new FieldDataType();
            fieldDbtype.DataType = this.DbType;
            fieldDbtype.Nullible = this.AllowNull;
            fieldDbtype.Precision = this.Precision;
            fieldDbtype.Provider = provider;
            fieldDbtype.Scale = this.Scale;
            fieldDbtype.Size = this.Size;
            return fieldDbtype;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public long GetMaxFieldSize()
        {

            long maxSize = 4;
            switch (this.DbType)
            {
                case SqlDbType.BigInt:
                    maxSize = 8;
                    break;
                case SqlDbType.Binary:
                    maxSize = 4000000000;
                    break;
                case SqlDbType.Bit:
                    maxSize = 1;
                    break;
                case SqlDbType.Char:
                    maxSize = this.Size;
                    break;
                case SqlDbType.DateTime:
                    maxSize = 2;
                    break;
                case SqlDbType.Decimal:
                    maxSize = 8;
                    break;
                case SqlDbType.Float:
                    maxSize = 4;
                    break;
                case SqlDbType.Image:
                    maxSize = 4000000000;
                    break;
                case SqlDbType.Int:
                    maxSize = 4;
                    break;
                case SqlDbType.Money:
                    maxSize = 2;
                    break;
                case SqlDbType.NChar:
                    maxSize = this.Size * 2;
                    break;
                case SqlDbType.NText:
                    maxSize = 4000000000;
                    break;
                case SqlDbType.NVarChar:
                    maxSize = this.Size * 2;
                    break;
                case SqlDbType.Real:
                    maxSize = 8;
                    break;
                case SqlDbType.SmallDateTime:
                    maxSize = 1;
                    break;
                case SqlDbType.SmallInt:
                    maxSize = 2;
                    break;
                case SqlDbType.SmallMoney:
                    maxSize = 1;
                    break;
                case SqlDbType.Text:
                    maxSize = 4000000000;
                    break;
                case SqlDbType.Timestamp:
                    maxSize = 2;
                    break;
                case SqlDbType.TinyInt:
                    maxSize = 1;
                    break;
                case SqlDbType.Udt:
                    maxSize = 2;
                    break;
                case SqlDbType.UniqueIdentifier:
                    maxSize = 2;
                    break;
                case SqlDbType.VarBinary:
                    maxSize = 4000000000;
                    break;
                case SqlDbType.VarChar:
                    maxSize = this.Size;
                    break;
                case SqlDbType.Variant:
                    maxSize = 4000000000;
                    break;
                case SqlDbType.Xml:
                    maxSize = 4000000000;
                    break;

                default:
                    maxSize = 2;
                    break;
            }
            return maxSize;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(object obj)
        {
            if (obj.GetType() == typeof(TableColumn))
            {
                TableColumn otherCol = (TableColumn) obj;
                if (this.Ordinal == otherCol.Ordinal)
                {
                    return this.ColumnName.ToLower().CompareTo(otherCol.ColumnName.ToLower());
                }
                else
                {
                    return this.Ordinal.CompareTo(otherCol.Ordinal);
                }
            }
            else
                return 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="col1"></param>
        /// <param name="col2"></param>
        /// <returns></returns>
        public static bool IsEqual(TableColumn col1, TableColumn col2)
        {
            if (col1.ColumnName.ToLower() != col2.ColumnName.ToLower())
            {
                return false;
            }
            //if (col1.DbType != col2.DbType)
            //    return false;
            //if (col1.Size != col2.Size)
            //    return false;
            //if (col1.Scale != col2.Scale)
            //    return false;
            //if (col1.Precision != col2.Precision)
            //    return false;
            return true;
        }
    }
}
